<script setup>


import {ref} from "vue";
import {UploadFilled} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";
import {useOcrStore} from "@/stores/OcrStore.js";
import Result from "@/views/BdClientView/OCR/components/result.vue";


const ocrStore = useOcrStore();
const flag = ref(true);//标记请求是否完成
const dialogFormVisible = ref(false)
let fileList = ref([])


function handleFileSelect(file) {
  fileList.value.push(file)
  console.log("fileList", fileList.value)
}

async function BdOcrs() {
  console.log("检测fileList", fileList.value)
  if (fileList.value.length === 0) {
    ElMessage({
      type: "error",
      message: "请先上传图片"
    })
  } else {
    ElMessage({
      message: '图片上传成功，请稍后',
      type:'success',
      duration: 2000,  //等待2秒后关闭
    })

    await ocrStore.getGeneralOcrTexts(fileList.value)

    if (ocrStore.generalOcrResultList.length !== 0) {

      ElMessage({
        message: '识别成功',
        type: 'success',
        duration: 2000,  //等待2秒后关闭
      })
      flag.value = false
      localStorage.setItem("flag", flag.value)
      console.log("总答案为：", ocrStore.generalOcrResultList)
      //请求完成之后删除请求文件
      fileList.value = []

    } else {
      console.log("请求失败")
      ElMessage({
        message: '请求失败',
        type: 'error',
        duration: 2000,  //等待2秒后关闭
      })
    }

  }
}


</script>

<template>
  <div style="text-align: center">

    <p class="title">高精度文字识别</p>

    <el-upload
        action=""
        :auto-upload="false"
        class="upload-demo"
        drag
        multiple
        :on-change="handleFileSelect"
    >
      <el-icon class="el-icon--upload">
        <upload-filled/>
      </el-icon>
      <div class="el-upload__text">
        Drop file here or <em>click to upload</em>
      </div>
      <template #tip>
        <div class="el-upload__tip">
          jpg/png photos with a size less than 500kb
        </div>
      </template>
    </el-upload>


    <div class="button-container">
      <el-button class="custom-button" style="margin-right: 500px" @click="BdOcrs()">上传识别</el-button>
      <el-button class="custom-button" @click="dialogFormVisible = true" :disabled="flag">查看结果</el-button>
    </div>


  </div>


  <div class="box">
    <el-dialog class="results" v-model="dialogFormVisible" title="识别结果" width="888">
      <!--   显示结果   -->
      <result></result>
    </el-dialog>
  </div>

</template>


<style scoped>
/* 渐变背景 */
.container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f0f8ff 0%, #e6f3ff 100%);
  padding: 40px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 上传区域容器 */
.upload-wrapper {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 48px;
  box-shadow: 0 8px 24px rgba(32, 120, 232, 0.08);
  width: 100%;
  max-width: 800px;
  text-align: center;
}

/* 自定义上传样式 */
.custom-upload {
  :deep(.el-upload-dragger) {
    background: rgba(240, 248, 255, 0.6);
    border: 2px dashed #a0cfff;
    border-radius: 12px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    &:hover {
      border-color: #409eff;
      background: rgba(64, 158, 255, 0.08);
      transform: translateY(-2px);
    }
  }

  :deep(.el-upload__tip) {
    color: #7c8ba1;
    margin-top: 12px;
  }
}

.upload-icon {
  font-size: 56px;
  color: #409eff;
  margin-bottom: 20px;
  filter: drop-shadow(0 2px 4px rgba(64, 158, 255, 0.2));
}

.upload-text {
  .main-text {
    color: #2c3e50;
    font-size: 18px;
    font-weight: 500;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
  }

  .sub-text {
    color: #7c8ba1;
    font-size: 14px;
  }
}

/* 操作按钮 */
.action-btn {
  margin-top: 32px;
  width: 240px;
  height: 52px;
  background: linear-gradient(135deg, #6aa8ff 0%, #409eff 100%);
  color: white;
  font-size: 16px;
  font-weight: 500;
  border: none;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.25);
  transition: all 0.3s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(64, 158, 255, 0.35);
  }

  &:active {
    transform: translateY(1px);
  }
}

/* 结果查看按钮 */
.result-btn {
  margin-top: 32px;
  padding: 14px 36px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid #409eff;
  color: #409eff;
  border-radius: 10px;
  transition: all 0.3s;

  &:hover {
    background: #409eff;
    color: white;

    .result-icon {
      transform: translateX(4px);
    }
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
    border-color: #c0c4cc;
    color: #c0c4cc;
  }
}

.result-icon {
  margin-left: 8px;
  transition: transform 0.3s;
}

/* 弹窗样式优化 */
:deep(.result-dialog) {
  border-radius: 16px;

  .el-dialog__header {
    padding: 24px;
    border-bottom: 1px solid #e4e7ed;
  }

  .el-dialog__title {
    color: #2c3e50;
    font-size: 20px;
    font-weight: 600;
  }

  .el-dialog__body {
    padding: 24px;
    background: #f8fafc;
  }
}

.upload-demo {
  border: 2px solid #87cff1; /* 淡蓝色边框 */
  padding: 20px; /* 内边距，使内容与边框之间有空间 */
  border-radius: 8px; /* 圆角边框 */
  background-color: #ffffff; /* 背景颜色为白色 */
  max-width: 90%; /* 设置最大宽度 */
  min-height: 200px; /* 设置最小高度 */
  height: auto;
  margin: 20px auto; /* 水平居中，上下外边距为20px */
}


.custom-button {
  margin: 30px 0 auto auto; /* 水平居中，上下外边距为20px */
  width: 200px;
  height: 50px;
  /* 背景颜色和渐变 */
  background: linear-gradient(to right, #63c6ff, #007bff); /* 从浅蓝色到深蓝色的渐变 */
  color: #ffffff; /* 文字颜色为白色 */
  border: none; /* 去掉默认边框 */
  border-radius: 5px; /* 圆角边框 */
  padding: 12px 24px; /* 增加内边距，使按钮看起来更大 */
  font-size: 16px; /* 字体大小 */
  cursor: pointer; /* 鼠标悬停时显示手型 */
  transition: all 0.3s ease; /* 添加过渡效果 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}

.custom-button:hover {
  /* 鼠标悬停时的样式 */
  background: linear-gradient(to right, #007bff, #0056b3); /* 改变背景颜色 */
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2); /* 更强的阴影效果 */
  transform: translateY(-2px); /* 稍微上移 */
}

.custom-button:active {
  /* 按钮按下时的样式 */
  transform: translateY(0); /* 恢复原位 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 较弱的阴影效果 */
}

.button-container {
  text-align: center;
}

.title {
  font-size: 2.5rem; /* 增大字体大小 */
  font-weight: bold; /* 加粗字体 */
  color: #000000;
  text-align: center; /* 居中对齐 */
  margin-bottom: 20px; /* 增加与下方内容的间距 */
  -webkit-background-clip: text;
  display: inline-block;
  padding: 10px 20px; /* 增加内边距 */
  border-radius: 10px; /* 添加圆角 */
}


</style>